﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using WLYT.Admin.WebApi.Component;
using WLYT.Admin.WebApi.Models;
using WLYT.Admin.WebApi.Models.Auth;
using WLYT.Application.Interfaces;
using WLYT.Common;
using WLYT.Domain.Model;

namespace WLYT.Admin.WebApi.Controllers
{
    [System.Web.Http.Description.ApiExplorerSettings(IgnoreApi = true)]
    public class RouteController : BaseController
    {
        private readonly ISys_MenusService SysMenusService;

        public RouteController(ISys_MenusService sysMenusService)
        {
            SysMenusService = sysMenusService;
        }
        [HttpGet]
        [Authentication]
        [Route("api/routes")]
        public async Task<RouteData.Routes> GenerateRouters()
        {
            List<RouteData.RouteItem> children = await RouteTreeLoad(CurrentUser.Roles.Split(',').ToList());
            RouteData.Routes mockRouters = new RouteData.Routes()
            {
                data = new List<RouteData.RouteItem>
                {
                    new RouteData.RouteItem
                    {
                        router = "root",
                        children =  children
                    }
                }
            };

            return mockRouters;
        }

        private async Task<List<RouteData.RouteItem>> RouteTreeLoad(List<string> roles)
        {
            IList<Sys_Menus> menus = await SysMenusService.GetAll();

            IList<Sys_Menus> parentRouteData = menus.Where(r => r.MenuParentId == 0).OrderBy(m => m.Sort).ToList();

            List<RouteData.RouteItem> routeData = new List<RouteData.RouteItem>();

            foreach (RouteData.RouteItem item in parentRouteData.Select(m => new RouteData.RouteItem
            {
                router = m.MenuPageUrl,
                id = m.Id,
                children = new List<RouteData.RouteItem>()
            }))
            {
                RouteChildListLoad(item, roles, menus);
                if (item.children.Count > 0 || item.router == "home")
                {
                    routeData.Add(item);
                }
            }
            return routeData;
        }

        private void RouteChildListLoad(RouteData.RouteItem sItem, List<string> roles, IList<Sys_Menus> cacheRouteData)
        {

            List<Sys_Menus> childRoutes;
            if (roles.Any(item => item == "*"))
            {
                childRoutes = cacheRouteData.Where(r => r.MenuParentId == sItem.id).OrderBy(s => s.Sort).ToList();
            }
            else
            {
                childRoutes = cacheRouteData.Where(r => r.MenuParentId == sItem.id && roles.Contains(r.MenuPageUrl)).OrderBy(s => s.Sort).ToList();
            }
            foreach (RouteData.RouteItem item in childRoutes.Select(item => new RouteData.RouteItem
            {
                id = item.Id,
                router = item.MenuPageUrl,
                name = item.MenuName,
                children = new List<RouteData.RouteItem>(),
                authority = "*"
            }))
            {
                RouteChildListLoad(item, roles, cacheRouteData);
                sItem.children.Add(item);
            }
        }
    }
}